import { ElMessage } from "element-plus";
export const districtString = (district: string): string => {
  switch (district) {
    case "1":
      return "重庆-联通";
    case "2":
      return "广州-联通";
    case "3":
      return "成都-移动";
    case "4":
      return "山东-联通";
    case "5":
      return "河南-电信";
    case "6":
      return "云南-电信";
    case "7":
      return "上海-联通";
    case "8":
      return "浙江-移动";
    case "9":
      return "云南-移动";
    default:
      return "未知地区";
  }
};

export const fixImageUrl = (url?: any): string => {
  if (!url) return "";
  return url.includes("http") ? url : `https://admin.hannuokeji.com${url}`;
};

export const imageFile = (path: any) => {
  return path.split(",").map((path: any) => {
    return { url: fixImageUrl(path), name: path.split("/").pop() };
  });
};

export const underscoreToCamel = (obj: any): any => {
  if (!obj || typeof obj !== "object") return obj;

  if (Array.isArray(obj)) {
    return obj.map(item => underscoreToCamel(item));
  }

  const result: Record<string, any> = {};
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      const camelKey = key.replace(/_([a-z])/g, (_, letter) =>
        letter.toUpperCase()
      );
      result[camelKey] = underscoreToCamel(obj[key]); // 递归处理嵌套对象或数组
    }
  }
  return result;
};

export const stringNotNull = (value: any): string => {
  return value === null || value === undefined || value.trim() === ""
    ? ""
    : value;
};

export const numberNotNull = (value: any): number => {
  return value === null || value === undefined ? 0 : value;
};

const baseUrl = import.meta.env.VITE_API_BASE_URL;
export const handleImagesUpload = async (params: {
  file: File;
  onSuccess: (response: any) => void;
  onError: (error: any) => void;
}) => {
  const { file, onSuccess, onError } = params;
  const formData = new FormData();
  formData.append("file", file); // 将文件添加到 FormData

  try {
    const response = await fetch(`${baseUrl}/common/upload/image`, {
      method: "POST",
      body: formData,
      // 监听上传进度
      signal: AbortSignal.timeout(60000) // 设置 60 秒超时
    });

    const result = await response.json();
    if (result.code !== 0) {
      throw new Error("上传失败");
    }

    onSuccess(result); // 上传成功回调
  } catch (error) {
    onError(error); // 上传失败回调
    ElMessage.error("图片上传失败，请重试");
  }
};

export const handleImagesUpload2 = async (params: {
  file: File;
  onSuccess: (response: any) => void;
  onError: (error: any) => void;
}) => {
  const { file, onSuccess, onError } = params;
  const formData = new FormData();
  formData.append("file", file); // 将文件添加到 FormData

  try {
    const response = await fetch(`${baseUrl}/common/upload/image`, {
      method: "POST",
      body: formData,
      // 监听上传进度
      signal: AbortSignal.timeout(60000) // 设置 60 秒超时
    });

    const result = await response.json();
    if (result.code !== 0) {
      throw new Error("上传失败");
    }

    onSuccess(result); // 上传成功回调
  } catch (error) {
    onError(error); // 上传失败回调
    ElMessage.error("图片上传失败，请重试");
  }
};

export const handleImagesUploadEditor = async (file: File) => {
  const formData = new FormData();
  formData.append("file", file); // 将文件添加到 FormData

  try {
    const response = await fetch(`${baseUrl}/common/upload/image`, {
      method: "POST",
      body: formData,
      // 监听上传进度
      signal: AbortSignal.timeout(60000) // 设置 60 秒超时
    });

    const result = await response.json();
    if (result.code !== 0) {
      throw new Error("上传失败");
    }
    return result;
  } catch (error) {
    ElMessage.error("图片上传失败，请重试");
    throw new Error("图片上传失败");
  }
};

export const isEmpty = (value: any): boolean => {
  return (
    value === undefined ||
    value === null ||
    value === "" ||
    (typeof value === "string" && value.trim() === "")
  );
};

export const formatDate = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  return `${year}年${month}月${day}日 ${hours}:${minutes}`;
};

// 会议/旅游是否可选择退款
export const canMeetingFundBack = (row: any) => {
  return !(row.auState == 0 || row.auState == 4);
};
